#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

/*
 * ./ledtest /dev/myled 100
 */
int main(int argc, char **argv)
{
	int fd;
	char status;
	int sleep, count;
	
	sleep = 1000; // 默认值
	/* 1. 判断参数 */
	if (argc < 2) 
	{
		printf("Usage: %s 100 (unit:ms)\n", argv[0]);
		return -1;
	}

	sleep = atoi(argv[2]);
	/* 2. 打开文件 */
	fd = open(argv[1], O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", argv[1]);
		return -1;
	}
	printf("sleep recycle %d ms\n", sleep);
	/* 3. 写文件或读文件 */
	while(1)
	{
		count++;
		status = (count%2);
		printf("Running count [%d], status [%d]\r\n", count, status);
		write(fd, &status, 1);
		usleep(sleep*1000);
	}
	
	close(fd);
	
	return 0;
}


